// 3.11 终止session（停止串流）
use reqwest::Client;
use crate::api::get_session_id;
use anyhow::{Result,anyhow};
use crate::api::get_stream_token::get_stream_token;
pub async fn close_session() -> Result<()>{
    let client = Client::new();
    let session_id = get_session_id::get_session_id(None).await.unwrap();
    // 设置会话状态为关闭
    get_session_id::set_session_id_status(false);
    let url = format!("https://xhome.gssv-play-prod.xboxlive.com/v5/sessions/home/{}",session_id);
    let stream_token = get_stream_token().await.unwrap();
    let Authorization = format!("Bearer {}", stream_token);
    let response = client.delete(url)
        .header("Authorization", Authorization)
        .header("Content-Type", "application/json")
        .send().await.unwrap();
    let response_code = response.status().as_u16();
    if (response_code < 200 || response_code > 299) {
        return Err(anyhow!("Failed to close session. Response code: {response_code}"));
    }
    return Ok(());
}